import cv2
import os
import numpy as np
import processImage
import Home
import test
import numpy as np
np.set_printoptions(threshold=np.inf)
## 读取图像，解决imread不能读取中文路径的问题
def cv_imread(filePath):
   # cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1) -1是4通道
    cv_img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8),-1)
    ## imdecode读取的是rgb，如果后续需要opencv处理的话，需要转换成bgr，转换后图片颜色会变化
    ##cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)
    return cv_img



def OpenFile(RootPath):

    similarity = []
    diff_name=[]
    similarity2 = []
    list = os.listdir(RootPath)  # 列出文件夹下所有的目录与文件
    count=0
    for file in list[:1000]:
        cv2.destroyAllWindows()
        Home.image_name=file
        fileName = os.path.join(RootPath,file)
        count=count+1

        image=cv_imread(fileName+"\\draw.png")
        draw_mask=cv_imread(fileName+"\\draw_mask.png")
        image = image[:, :, :3]
        Home.original_image = image.copy()

        # print(fileName+"\\draw.png")
        end, down, high_max = processImage.search_number(image.copy())  # 找数
        image, start, end, zero_mid, down, up, number_high=processImage.local_number_down(image.copy())
#反相
        # image_save=image.copy()
        # image_fan = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # if np.sum(image_fan[5:10, 5:10]) > 4000:
        #     image[:, :, :] = 255 - image[:, :, :]
        image_black=image.copy()
        image_black[:,:,:]=0


        img_half1=processImage.colour_sub(image.copy() )



        result=processImage.get_line2(image.copy(),zero_mid,down-high_max*3)#找图
        cv2.imshow("step2_result", result)
        line_image=processImage.second_search(result.copy(),image.copy())
        print(line_image.shape,img_half1.shape)
        img_all=cv2.add(line_image,img_half1)
        cv2.imshow("show_all",img_all)
        cv2.waitKey(0)



        # rat=test.differenec_rat(result,draw_mask)
        # if rat<0.5:
        #     diff_name.append(file)
        # #rat_origion=test.differenec_rat(image,draw_mask)
        # similarity.append(rat)
        #similarity2.append(rat_origion)
        if count%100==0:
            print("进度：",count/100,"/",range//100,",-》",len(diff_name)/range)
    print(len(diff_name))
    #print(similarity)
    #print(similarity2)
        #processImage.search_origin_point(image)

# def OpenFile2(RootPath):
#     list = os.listdir(RootPath)  # 列出文件夹下所有的目录与文件
#     for file in list:
#         Home.image_name = file
#         fileName = os.path.join(RootPath, file)
#         image = cv_imread(fileName + "\\draw.png")
#
#
#
#
#
#
#
#         image=image[:,:,:3]
#         #print("why 4?",image.shape,image[0][0])
#         # 调整宽度，使得变窄，容易读取数字
#         #image = cv2.resize(image, None, fx=500 / image.shape[1], fy=1, interpolation=cv2.INTER_LINEAR)
#         # print(fileName+"\\draw.png")
#         processImage.get_line2(image)
#         #processImage.search_line(image)
#
#         # processImage.search_origin_point(image)





    return 5;



if __name__ == '__main__':

    #
    # OpenFile("D:\Shanmh\DeskTop\实验")
    #OpenFile("D:\桌面文件\毕设\服务外包大赛数据更新\训练数据\img_train_折线")
    OpenFile("D:\桌面文件\毕设\服务外包大赛数据更新\训练数据\img_train_曲线")
    image=cv2.imread("images/black.png")




    '''
    #image=cv_imread(os.path.join("D:\系统文件\桌面\img_折线","0\draw.png"))
    print(image.shape[0],image.shape[1])
    print(image[0][0])
    #image[:,:]=255-image[:,:]
    image = cv2.resize(image,None,fx=500/image.shape[1],fy=1, interpolation=cv2.INTER_LINEAR)
    #newimage2 = cv2.resize(image,int(image.shape[1]/5),int(image.shape[0]/5),cv2.INTER_LINEAR)
    processImage.search_number(image)
    '''

